﻿using Microsoft.AspNetCore.Mvc;
using Minio;
using Minio.DataModel;
using Minio.Exceptions;
using minioClient.ExUtil;
using minioClient.Models;
using System.Diagnostics;

namespace minioClient.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IHttpClientFactory _httpClientFactory;

        public HomeController(ILogger<HomeController> logger/*, IHttpClientFactory httpClientFactory*/)
        {
            _logger = logger;
            //_httpClientFactory = httpClientFactory;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        //[HttpPost("Upload")]
        //[HttpPost]
        public async Task<IActionResult> Upload(List<IFormFile> files)
        {
            // 2.1 遍历所有文件
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    var endpoint = "play.min.io";
                    var accessKey = "Q3AM3UQ867SPQQA43P2F";
                    var secretKey = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG";

                    //var endpoint = "127.0.0.1:9000";
                    //var accessKey = "minioadmin";
                    //var secretKey = "minioadmin";

                    // 2.1 创建MinioClient客户端
                    MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);

                    var buckName = "blog";
                    //判断桶是否存在，如果不存在则创建桶，否则上传文件会异常
                    var exists = await MinioHelper.BucketExists(minioClient, buckName);// 方式一，使用帮助类
                    ///var a = await minioClient.BucketExistsAsync(buckName);// 方式二，使用客户端自带方法
                    if (!exists)
                    {
                        // 2.2 创建文件桶
                        minioClient.MakeBucketAsync(buckName).Wait();
                    }
                    var pa = "MinioUpload/";
                    // 2.3 上传文件
                    minioClient.PutObjectAsync(buckName, pa + formFile.FileName, formFile.OpenReadStream(), formFile.Length).Wait();

                    _logger.LogInformation($"文件:{pa + formFile.FileName}上传到MinIO成功");
                }
            }

            return new JsonResult("上传成功");
        }

        // Download object from bucket into local file
        //public async static Task Run(MinioClient minio,
        //                             string bucketName = "my-bucket-name",
        //                             string objectName = "my-object-name",
        //                             string fileName = "local-filename",
        //                             ServerSideEncryption sse = null)
        //{
        //    try
        //    {
        //        Console.WriteLine("Running example for API: GetObjectAsync");
        //        System.IO.File.Delete(fileName);
        //        GetObjectArgs args = new GetObjectArgs()
        //                                        .WithBucket(bucketName)
        //                                        .WithObject(objectName)
        //                                        .WithFile(fileName)
        //                                        .WithServerSideEncryption(sse);
        //        await minio.GetObjectAsync(args).ConfigureAwait(false);
        //        Console.WriteLine($"Downloaded the file {fileName} from bucket {bucketName}");
        //        Console.WriteLine();
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine($"[Bucket]  Exception: {e}");
        //    }
        //}

        /// <summary>
        /// 文件下载
        /// </summary>
        /// <returns></returns>
        //[HttpPost("Upload")]
        //[HttpPost]
        public IActionResult Download(string fileName)
        {
            FileStreamResult fileStreamResult = null;
            // 1、创建MioIO客户端
            MinioClient minioClient = new MinioClient("127.0.0.1:9000", "minioadmin", "minioadmin");

            var imgStream = new MemoryStream();
            // 2、下载图片
            minioClient.GetObjectAsync("products", fileName, stream => stream.CopyTo(imgStream)).Wait();
            imgStream.Position = 0;

            //fileStreamResult = new FileStreamResult(imgStream, GetContentType(fileName));// 原版
            fileStreamResult = new FileStreamResult(imgStream, GetContentType_New(fileName));// 新版本
            return fileStreamResult;
        }


        private static readonly Lazy<IDictionary<string, string>> _contentTypeMap = new Lazy<IDictionary<string, string>>(AddContentTypeMappings);

        /// <summary>
        /// 获取文件后缀
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static string GetContentType_New(string fileName)
        {
            string extension = null;
            try
            {
                extension = Path.GetExtension(fileName);
            }
            catch
            {
            }

            if (string.IsNullOrEmpty(extension))
            {
                return "application/octet-stream";
            }

            return _contentTypeMap.Value.TryGetValue(extension, out string contentType)
                ? contentType
                : "application/octet-stream";
        }

        /// <summary>
        /// 根据后缀获取对应的contentType字符串
        /// </summary>
        /// <returns></returns>
        private static IDictionary<string, string> AddContentTypeMappings()
        {
            return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                {".323", "text/h323"},
                {".3g2", "video/3gpp2"},
                {".3gp", "video/3gpp"},
                {".3gp2", "video/3gpp2"},
                {".3gpp", "video/3gpp"},
                {".7z", "application/x-7z-compressed"},
                {".aa", "audio/audible"},
                {".AAC", "audio/aac"},
                {".aax", "audio/vnd.audible.aax"},
                {".ac3", "audio/ac3"},
                {".accda", "application/msaccess.addin"},
                {".accdb", "application/msaccess"},
                {".accdc", "application/msaccess.cab"},
                {".accde", "application/msaccess"},
                {".accdr", "application/msaccess.runtime"},
                {".accdt", "application/msaccess"},
                {".accdw", "application/msaccess.webapplication"},
                {".accft", "application/msaccess.ftemplate"},
                {".acx", "application/internet-property-stream"},
                {".AddIn", "text/xml"},
                {".ade", "application/msaccess"},
                {".adobebridge", "application/x-bridge-url"},
                {".adp", "application/msaccess"},
                {".ADT", "audio/vnd.dlna.adts"},
                {".ADTS", "audio/aac"},
                {".ai", "application/postscript"},
                {".aif", "audio/aiff"},
                {".aifc", "audio/aiff"},
                {".aiff", "audio/aiff"},
                {".air", "application/vnd.adobe.air-application-installer-package+zip"},
                {".amc", "application/mpeg"},
                {".anx", "application/annodex"},
                {".apk", "application/vnd.android.package-archive" },
                {".application", "application/x-ms-application"},
                {".art", "image/x-jg"},
                {".asa", "application/xml"},
                {".asax", "application/xml"},
                {".ascx", "application/xml"},
                {".asf", "video/x-ms-asf"},
                {".ashx", "application/xml"},
                {".asm", "text/plain"},
                {".asmx", "application/xml"},
                {".aspx", "application/xml"},
                {".asr", "video/x-ms-asf"},
                {".asx", "video/x-ms-asf"},
                {".atom", "application/atom+xml"},
                {".au", "audio/basic"},
                {".avi", "video/x-msvideo"},
                {".axa", "audio/annodex"},
                {".axs", "application/olescript"},
                {".axv", "video/annodex"},
                {".bas", "text/plain"},
                {".bcpio", "application/x-bcpio"},
                {".bmp", "image/bmp"},
                {".c", "text/plain"},
                {".caf", "audio/x-caf"},
                {".calx", "application/vnd.ms-office.calx"},
                {".cat", "application/vnd.ms-pki.seccat"},
                {".cc", "text/plain"},
                {".cd", "text/plain"},
                {".cdda", "audio/aiff"},
                {".cdf", "application/x-cdf"},
                {".cer", "application/x-x509-ca-cert"},
                {".cfg", "text/plain"},
                {".class", "application/x-java-applet"},
                {".clp", "application/x-msclip"},
                {".cmd", "text/plain"},
                {".cmx", "image/x-cmx"},
                {".cnf", "text/plain"},
                {".cod", "image/cis-cod"},
                {".config", "application/xml"},
                {".contact", "text/x-ms-contact"},
                {".coverage", "application/xml"},
                {".cpio", "application/x-cpio"},
                {".cpp", "text/plain"},
                {".crd", "application/x-mscardfile"},
                {".crl", "application/pkix-crl"},
                {".crt", "application/x-x509-ca-cert"},
                {".cs", "text/plain"},
                {".csdproj", "text/plain"},
                {".csh", "application/x-csh"},
                {".csproj", "text/plain"},
                {".css", "text/css"},
                {".csv", "text/csv"},
                {".cxx", "text/plain"},
                {".datasource", "application/xml"},
                {".dbproj", "text/plain"},
                {".dcr", "application/x-director"},
                {".def", "text/plain"},
                {".der", "application/x-x509-ca-cert"},
                {".dgml", "application/xml"},
                {".dib", "image/bmp"},
                {".dif", "video/x-dv"},
                {".dir", "application/x-director"},
                {".disco", "text/xml"},
                {".divx", "video/divx"},
                {".dll", "application/x-msdownload"},
                {".dll.config", "text/xml"},
                {".dlm", "text/dlm"},
                {".doc", "application/msword"},
                {".docm", "application/vnd.ms-word.document.macroEnabled.12"},
                {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
                {".dot", "application/msword"},
                {".dotm", "application/vnd.ms-word.template.macroEnabled.12"},
                {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},
                {".dsw", "text/plain"},
                {".dtd", "text/xml"},
                {".dtsConfig", "text/xml"},
                {".dv", "video/x-dv"},
                {".dvi", "application/x-dvi"},
                {".dwf", "drawing/x-dwf"},
                {".dwg", "application/acad"},
                {".dxf", "application/x-dxf" },
                {".dxr", "application/x-director"},
                {".eml", "message/rfc822"},
                {".eot", "application/vnd.ms-fontobject"},
                {".eps", "application/postscript"},
                {".etl", "application/etl"},
                {".etx", "text/x-setext"},
                {".evy", "application/envoy"},
                {".exe.config", "text/xml"},
                {".fdf", "application/vnd.fdf"},
                {".fif", "application/fractals"},
                {".filters", "application/xml"},
                {".flac", "audio/flac"},
                {".flr", "x-world/x-vrml"},
                {".flv", "video/x-flv"},
                {".fsscript", "application/fsharp-script"},
                {".fsx", "application/fsharp-script"},
                {".generictest", "application/xml"},
                {".gif", "image/gif"},
                {".gpx", "application/gpx+xml"},
                {".group", "text/x-ms-group"},
                {".gsm", "audio/x-gsm"},
                {".gtar", "application/x-gtar"},
                {".gz", "application/x-gzip"},
                {".h", "text/plain"},
                {".hdf", "application/x-hdf"},
                {".hdml", "text/x-hdml"},
                {".hhc", "application/x-oleobject"},
                {".hlp", "application/winhlp"},
                {".hpp", "text/plain"},
                {".hqx", "application/mac-binhex40"},
                {".hta", "application/hta"},
                {".htc", "text/x-component"},
                {".htm", "text/html"},
                {".html", "text/html"},
                {".htt", "text/webviewhtml"},
                {".hxa", "application/xml"},
                {".hxc", "application/xml"},
                {".hxe", "application/xml"},
                {".hxf", "application/xml"},
                {".hxk", "application/xml"},
                {".hxt", "text/html"},
                {".hxv", "application/xml"},
                {".hxx", "text/plain"},
                {".i", "text/plain"},
                {".ico", "image/x-icon"},
                {".idl", "text/plain"},
                {".ief", "image/ief"},
                {".iii", "application/x-iphone"},
                {".inc", "text/plain"},
                {".ini", "text/plain"},
                {".inl", "text/plain"},
                {".ins", "application/x-internet-signup"},
                {".ipa", "application/x-itunes-ipa"},
                {".ipg", "application/x-itunes-ipg"},
                {".ipproj", "text/plain"},
                {".ipsw", "application/x-itunes-ipsw"},
                {".iqy", "text/x-ms-iqy"},
                {".isp", "application/x-internet-signup"},
                {".ite", "application/x-itunes-ite"},
                {".itlp", "application/x-itunes-itlp"},
                {".itms", "application/x-itunes-itms"},
                {".itpc", "application/x-itunes-itpc"},
                {".IVF", "video/x-ivf"},
                {".jar", "application/java-archive"},
                {".jck", "application/liquidmotion"},
                {".jcz", "application/liquidmotion"},
                {".jfif", "image/pjpeg"},
                {".jnlp", "application/x-java-jnlp-file"},
                {".jpe", "image/jpeg"},
                {".jpeg", "image/jpeg"},
                {".jpg", "image/jpeg"},
                {".js", "application/javascript"},
                {".json", "application/json"},
                {".jsx", "text/jscript"},
                {".jsxbin", "text/plain"},
                {".latex", "application/x-latex"},
                {".library-ms", "application/windows-library+xml"},
                {".lit", "application/x-ms-reader"},
                {".loadtest", "application/xml"},
                {".lsf", "video/x-la-asf"},
                {".lst", "text/plain"},
                {".lsx", "video/x-la-asf"},
                {".m13", "application/x-msmediaview"},
                {".m14", "application/x-msmediaview"},
                {".m1v", "video/mpeg"},
                {".m2t", "video/vnd.dlna.mpeg-tts"},
                {".m2ts", "video/vnd.dlna.mpeg-tts"},
                {".m2v", "video/mpeg"},
                {".m3u", "audio/x-mpegurl"},
                {".m3u8", "audio/x-mpegurl"},
                {".m4a", "audio/m4a"},
                {".m4b", "audio/m4b"},
                {".m4p", "audio/m4p"},
                {".m4r", "audio/x-m4r"},
                {".m4v", "video/x-m4v"},
                {".mac", "image/x-macpaint"},
                {".mak", "text/plain"},
                {".man", "application/x-troff-man"},
                {".manifest", "application/x-ms-manifest"},
                {".map", "text/plain"},
                {".master", "application/xml"},
                {".mbox", "application/mbox"},
                {".mda", "application/msaccess"},
                {".mdb", "application/x-msaccess"},
                {".mde", "application/msaccess"},
                {".me", "application/x-troff-me"},
                {".mfp", "application/x-shockwave-flash"},
                {".mht", "message/rfc822"},
                {".mhtml", "message/rfc822"},
                {".mid", "audio/mid"},
                {".midi", "audio/mid"},
                {".mk", "text/plain"},
                {".mmf", "application/x-smaf"},
                {".mno", "text/xml"},
                {".mny", "application/x-msmoney"},
                {".mod", "video/mpeg"},
                {".mov", "video/quicktime"},
                {".movie", "video/x-sgi-movie"},
                {".mp2", "video/mpeg"},
                {".mp2v", "video/mpeg"},
                {".mp3", "audio/mpeg"},
                {".mp4", "video/mp4"},
                {".mp4v", "video/mp4"},
                {".mpa", "video/mpeg"},
                {".mpe", "video/mpeg"},
                {".mpeg", "video/mpeg"},
                {".mpf", "application/vnd.ms-mediapackage"},
                {".mpg", "video/mpeg"},
                {".mpp", "application/vnd.ms-project"},
                {".mpv2", "video/mpeg"},
                {".mqv", "video/quicktime"},
                {".ms", "application/x-troff-ms"},
                {".msg", "application/vnd.ms-outlook"},
                {".mts", "video/vnd.dlna.mpeg-tts"},
                {".mtx", "application/xml"},
                {".mvb", "application/x-msmediaview"},
                {".mvc", "application/x-miva-compiled"},
                {".mxp", "application/x-mmxp"},
                {".nc", "application/x-netcdf"},
                {".nsc", "video/x-ms-asf"},
                {".nws", "message/rfc822"},
                {".oda", "application/oda"},
                {".odb", "application/vnd.oasis.opendocument.database"},
                {".odc", "application/vnd.oasis.opendocument.chart"},
                {".odf", "application/vnd.oasis.opendocument.formula"},
                {".odg", "application/vnd.oasis.opendocument.graphics"},
                {".odh", "text/plain"},
                {".odi", "application/vnd.oasis.opendocument.image"},
                {".odl", "text/plain"},
                {".odm", "application/vnd.oasis.opendocument.text-master"},
                {".odp", "application/vnd.oasis.opendocument.presentation"},
                {".ods", "application/vnd.oasis.opendocument.spreadsheet"},
                {".odt", "application/vnd.oasis.opendocument.text"},
                {".oga", "audio/ogg"},
                {".ogg", "audio/ogg"},
                {".ogv", "video/ogg"},
                {".ogx", "application/ogg"},
                {".one", "application/onenote"},
                {".onea", "application/onenote"},
                {".onepkg", "application/onenote"},
                {".onetmp", "application/onenote"},
                {".onetoc", "application/onenote"},
                {".onetoc2", "application/onenote"},
                {".opus", "audio/ogg"},
                {".orderedtest", "application/xml"},
                {".osdx", "application/opensearchdescription+xml"},
                {".otf", "application/font-sfnt"},
                {".otg", "application/vnd.oasis.opendocument.graphics-template"},
                {".oth", "application/vnd.oasis.opendocument.text-web"},
                {".otp", "application/vnd.oasis.opendocument.presentation-template"},
                {".ots", "application/vnd.oasis.opendocument.spreadsheet-template"},
                {".ott", "application/vnd.oasis.opendocument.text-template"},
                {".oxt", "application/vnd.openofficeorg.extension"},
                {".p10", "application/pkcs10"},
                {".p12", "application/x-pkcs12"},
                {".p7b", "application/x-pkcs7-certificates"},
                {".p7c", "application/pkcs7-mime"},
                {".p7m", "application/pkcs7-mime"},
                {".p7r", "application/x-pkcs7-certreqresp"},
                {".p7s", "application/pkcs7-signature"},
                {".pbm", "image/x-portable-bitmap"},
                {".pcast", "application/x-podcast"},
                {".pct", "image/pict"},
                {".pdf", "application/pdf"},
                {".pfx", "application/x-pkcs12"},
                {".pgm", "image/x-portable-graymap"},
                {".pic", "image/pict"},
                {".pict", "image/pict"},
                {".pkgdef", "text/plain"},
                {".pkgundef", "text/plain"},
                {".pko", "application/vnd.ms-pki.pko"},
                {".pls", "audio/scpls"},
                {".pma", "application/x-perfmon"},
                {".pmc", "application/x-perfmon"},
                {".pml", "application/x-perfmon"},
                {".pmr", "application/x-perfmon"},
                {".pmw", "application/x-perfmon"},
                {".png", "image/png"},
                {".pnm", "image/x-portable-anymap"},
                {".pnt", "image/x-macpaint"},
                {".pntg", "image/x-macpaint"},
                {".pnz", "image/png"},
                {".pot", "application/vnd.ms-powerpoint"},
                {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"},
                {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"},
                {".ppa", "application/vnd.ms-powerpoint"},
                {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"},
                {".ppm", "image/x-portable-pixmap"},
                {".pps", "application/vnd.ms-powerpoint"},
                {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"},
                {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"},
                {".ppt", "application/vnd.ms-powerpoint"},
                {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"},
                {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
                {".prf", "application/pics-rules"},
                {".ps", "application/postscript"},
                {".psc1", "application/PowerShell"},
                {".psess", "application/xml"},
                {".pst", "application/vnd.ms-outlook"},
                {".pub", "application/x-mspublisher"},
                {".pwz", "application/vnd.ms-powerpoint"},
                {".qht", "text/x-html-insertion"},
                {".qhtm", "text/x-html-insertion"},
                {".qt", "video/quicktime"},
                {".qti", "image/x-quicktime"},
                {".qtif", "image/x-quicktime"},
                {".qtl", "application/x-quicktimeplayer"},
                {".ra", "audio/x-pn-realaudio"},
                {".ram", "audio/x-pn-realaudio"},
                {".rar", "application/x-rar-compressed"},
                {".ras", "image/x-cmu-raster"},
                {".rat", "application/rat-file"},
                {".rc", "text/plain"},
                {".rc2", "text/plain"},
                {".rct", "text/plain"},
                {".rdlc", "application/xml"},
                {".reg", "text/plain"},
                {".resx", "application/xml"},
                {".rf", "image/vnd.rn-realflash"},
                {".rgb", "image/x-rgb"},
                {".rgs", "text/plain"},
                {".rm", "application/vnd.rn-realmedia"},
                {".rmi", "audio/mid"},
                {".rmp", "application/vnd.rn-rn_music_package"},
                {".roff", "application/x-troff"},
                {".rpm", "audio/x-pn-realaudio-plugin"},
                {".rqy", "text/x-ms-rqy"},
                {".rtf", "application/rtf"},
                {".rtx", "text/richtext"},
                {".ruleset", "application/xml"},
                {".s", "text/plain"},
                {".safariextz", "application/x-safari-safariextz"},
                {".scd", "application/x-msschedule"},
                {".scr", "text/plain"},
                {".sct", "text/scriptlet"},
                {".sd2", "audio/x-sd2"},
                {".sdp", "application/sdp"},
                {".searchConnector-ms", "application/windows-search-connector+xml"},
                {".setpay", "application/set-payment-initiation"},
                {".setreg", "application/set-registration-initiation"},
                {".settings", "application/xml"},
                {".sgimb", "application/x-sgimb"},
                {".sgml", "text/sgml"},
                {".sh", "application/x-sh"},
                {".shar", "application/x-shar"},
                {".shtml", "text/html"},
                {".sit", "application/x-stuffit"},
                {".sitemap", "application/xml"},
                {".skin", "application/xml"},
                {".skp", "application/x-koan" },
                {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"},
                {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"},
                {".slk", "application/vnd.ms-excel"},
                {".sln", "text/plain"},
                {".slupkg-ms", "application/x-ms-license"},
                {".smd", "audio/x-smd"},
                {".smx", "audio/x-smd"},
                {".smz", "audio/x-smd"},
                {".snd", "audio/basic"},
                {".snippet", "application/xml"},
                {".sol", "text/plain"},
                {".sor", "text/plain"},
                {".spc", "application/x-pkcs7-certificates"},
                {".spl", "application/futuresplash"},
                {".spx", "audio/ogg"},
                {".src", "application/x-wais-source"},
                {".srf", "text/plain"},
                {".SSISDeploymentManifest", "text/xml"},
                {".ssm", "application/streamingmedia"},
                {".sst", "application/vnd.ms-pki.certstore"},
                {".stl", "application/vnd.ms-pki.stl"},
                {".sv4cpio", "application/x-sv4cpio"},
                {".sv4crc", "application/x-sv4crc"},
                {".svc", "application/xml"},
                {".svg", "image/svg+xml"},
                {".swf", "application/x-shockwave-flash"},
                {".step", "application/step"},
                {".stp", "application/step"},
                {".t", "application/x-troff"},
                {".tar", "application/x-tar"},
                {".tcl", "application/x-tcl"},
                {".testrunconfig", "application/xml"},
                {".testsettings", "application/xml"},
                {".tex", "application/x-tex"},
                {".texi", "application/x-texinfo"},
                {".texinfo", "application/x-texinfo"},
                {".tgz", "application/x-compressed"},
                {".thmx", "application/vnd.ms-officetheme"},
                {".tif", "image/tiff"},
                {".tiff", "image/tiff"},
                {".tlh", "text/plain"},
                {".tli", "text/plain"},
                {".tr", "application/x-troff"},
                {".trm", "application/x-msterminal"},
                {".trx", "application/xml"},
                {".ts", "video/vnd.dlna.mpeg-tts"},
                {".tsv", "text/tab-separated-values"},
                {".ttf", "application/font-sfnt"},
                {".tts", "video/vnd.dlna.mpeg-tts"},
                {".txt", "text/plain"},
                {".uls", "text/iuls"},
                {".user", "text/plain"},
                {".ustar", "application/x-ustar"},
                {".vb", "text/plain"},
                {".vbdproj", "text/plain"},
                {".vbk", "video/mpeg"},
                {".vbproj", "text/plain"},
                {".vbs", "text/vbscript"},
                {".vcf", "text/x-vcard"},
                {".vcproj", "application/xml"},
                {".vcs", "text/plain"},
                {".vcxproj", "application/xml"},
                {".vddproj", "text/plain"},
                {".vdp", "text/plain"},
                {".vdproj", "text/plain"},
                {".vdx", "application/vnd.ms-visio.viewer"},
                {".vml", "text/xml"},
                {".vscontent", "application/xml"},
                {".vsct", "text/xml"},
                {".vsd", "application/vnd.visio"},
                {".vsi", "application/ms-vsi"},
                {".vsix", "application/vsix"},
                {".vsixlangpack", "text/xml"},
                {".vsixmanifest", "text/xml"},
                {".vsmdi", "application/xml"},
                {".vspscc", "text/plain"},
                {".vss", "application/vnd.visio"},
                {".vsscc", "text/plain"},
                {".vssettings", "text/xml"},
                {".vssscc", "text/plain"},
                {".vst", "application/vnd.visio"},
                {".vstemplate", "text/xml"},
                {".vsto", "application/x-ms-vsto"},
                {".vsw", "application/vnd.visio"},
                {".vsx", "application/vnd.visio"},
                {".vtx", "application/vnd.visio"},
                {".wav", "audio/wav"},
                {".wave", "audio/wav"},
                {".wax", "audio/x-ms-wax"},
                {".wbk", "application/msword"},
                {".wbmp", "image/vnd.wap.wbmp"},
                {".wcm", "application/vnd.ms-works"},
                {".wdb", "application/vnd.ms-works"},
                {".wdp", "image/vnd.ms-photo"},
                {".webarchive", "application/x-safari-webarchive"},
                {".webm", "video/webm"},
                {".webp", "image/webp"},
                {".webtest", "application/xml"},
                {".wiq", "application/xml"},
                {".wiz", "application/msword"},
                {".wks", "application/vnd.ms-works"},
                {".WLMP", "application/wlmoviemaker"},
                {".wlpginstall", "application/x-wlpg-detect"},
                {".wlpginstall3", "application/x-wlpg3-detect"},
                {".wm", "video/x-ms-wm"},
                {".wma", "audio/x-ms-wma"},
                {".wmd", "application/x-ms-wmd"},
                {".wmf", "application/x-msmetafile"},
                {".wml", "text/vnd.wap.wml"},
                {".wmlc", "application/vnd.wap.wmlc"},
                {".wmls", "text/vnd.wap.wmlscript"},
                {".wmlsc", "application/vnd.wap.wmlscriptc"},
                {".wmp", "video/x-ms-wmp"},
                {".wmv", "video/x-ms-wmv"},
                {".wmx", "video/x-ms-wmx"},
                {".wmz", "application/x-ms-wmz"},
                {".woff", "application/font-woff"},
                {".wpl", "application/vnd.ms-wpl"},
                {".wps", "application/vnd.ms-works"},
                {".wri", "application/x-mswrite"},
                {".wrl", "x-world/x-vrml"},
                {".wrz", "x-world/x-vrml"},
                {".wsc", "text/scriptlet"},
                {".wsdl", "text/xml"},
                {".wvx", "video/x-ms-wvx"},
                {".x", "application/directx"},
                {".xaf", "x-world/x-vrml"},
                {".xaml", "application/xaml+xml"},
                {".xap", "application/x-silverlight-app"},
                {".xbap", "application/x-ms-xbap"},
                {".xbm", "image/x-xbitmap"},
                {".xdr", "text/plain"},
                {".xht", "application/xhtml+xml"},
                {".xhtml", "application/xhtml+xml"},
                {".xla", "application/vnd.ms-excel"},
                {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"},
                {".xlc", "application/vnd.ms-excel"},
                {".xld", "application/vnd.ms-excel"},
                {".xlk", "application/vnd.ms-excel"},
                {".xll", "application/vnd.ms-excel"},
                {".xlm", "application/vnd.ms-excel"},
                {".xls", "application/vnd.ms-excel"},
                {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"},
                {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"},
                {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
                {".xlt", "application/vnd.ms-excel"},
                {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"},
                {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"},
                {".xlw", "application/vnd.ms-excel"},
                {".xml", "text/xml"},
                {".xmta", "application/xml"},
                {".xof", "x-world/x-vrml"},
                {".XOML", "text/plain"},
                {".xpm", "image/x-xpixmap"},
                {".xps", "application/vnd.ms-xpsdocument"},
                {".xrm-ms", "text/xml"},
                {".xsc", "application/xml"},
                {".xsd", "text/xml"},
                {".xsf", "text/xml"},
                {".xsl", "text/xml"},
                {".xslt", "text/xml"},
                {".xss", "application/xml"},
                {".xspf", "application/xspf+xml"},
                {".xwd", "image/x-xwindowdump"},
                {".z", "application/x-compress"},
                {".zip", "application/zip"}
            };
        }

        private static string GetContentType(string fileName)
        {
            if (fileName.Contains(".jpg"))
            {
                return "image/jpg";
            }
            else if (fileName.Contains(".jpg"))
            {
                return "image/jpg";
            }
            else if (fileName.Contains(".jpeg"))
            {
                return "image/jpeg";
            }
            else if (fileName.Contains(".png"))
            {
                return "image/png";
            }
            else if (fileName.Contains(".gif"))
            {
                return "image/gif";
            }
            else if (fileName.Contains(".pdf"))
            {
                return "application/pdf";
            }
            else if (fileName.Contains(".docx"))
            {
                return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            }
            else if (fileName.Contains(".doc"))
            {
                return "application/msword";
            }
            else if (fileName.Contains(".xlsx"))
            {
                return "application/xlsx";
            }
            else if (fileName.Contains(".rtf"))
            {
                return "application/rtf";
            }
            else
            {
                return "application/octet-stream";
            }
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        public class MinioHelper
        {

            #region 操作存储桶

            /// <summary>创建存储桶
            /// 创建存储桶
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="loc">可选参数</param>
            /// <returns></returns>
            public async static Task<bool> MakeBucket(MinioClient minio, string bucketName, string loc = "us-east-1")
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        throw new Exception(string.Format("存储桶[{0}]已存在", bucketName));
                    }
                    else
                    {
                        await minio.MakeBucketAsync(bucketName, loc);
                        flag = true;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>列出所有的存储桶
            /// 列出所有的存储桶
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <returns></returns>
            public async static Task<Tuple<bool, Minio.DataModel.ListAllMyBucketsResult>> ListBuckets(MinioClient minio)
            {
                bool flag = false;
                var list = new Minio.DataModel.ListAllMyBucketsResult();
                try
                {
                    list = await minio.ListBucketsAsync();
                    flag = true;
                    //foreach (var bucket in list.Buckets)
                    //{
                    //    Console.WriteLine($"{bucket.Name} {bucket.CreationDateDateTime}");
                    //}
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, list);
            }

            /// <summary>检查存储桶是否存在
            /// 检查存储桶是否存在
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            public async static Task<bool> BucketExists(MinioClient minio, string bucketName)
            {
                bool flag = false;
                try
                {
                    flag = await minio.BucketExistsAsync(bucketName);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>删除一个存储桶
            /// 删除一个存储桶
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            public async static Task<bool> RemoveBucket(MinioClient minio, string bucketName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.RemoveBucketAsync(bucketName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>列出存储桶里的对象
            /// 列出存储桶里的对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="prefix">对象的前缀</param>
            /// <param name="recursive">true代表递归查找，false代表类似文件夹查找，以'/'分隔，不查子文件夹</param>
            public static Tuple<bool, IObservable<Item>> ListObjects(MinioClient minio, string bucketName, string prefix = null, bool recursive = true)
            {
                bool flag = false;
                IObservable<Item> observable = null;
                try
                {
                    var found = minio.BucketExistsAsync(bucketName);
                    if (found.Result)
                    {
                        observable = minio.ListObjectsAsync(bucketName, prefix, recursive);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                    //IDisposable subscription = observable.Subscribe(
                    //    item => Console.WriteLine($"Object: {item.Key}"),
                    //    ex => Console.WriteLine($"OnError: {ex}"),
                    //    () => Console.WriteLine($"Listed all objects in bucket {bucketName}\n"));

                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, observable);
            }

            /// <summary>列出存储桶中未完整上传的对象
            /// 列出存储桶中未完整上传的对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="prefix">对象的前缀</param>
            /// <param name="recursive">true代表递归查找，false代表类似文件夹查找，以'/'分隔，不查子文件夹</param>
            /// <returns></returns>
            public static Tuple<bool, IObservable<Upload>> ListIncompleteUploads(MinioClient minio, string bucketName, string prefix = null, bool recursive = true)
            {
                bool flag = false;
                IObservable<Upload> observable = null;
                try
                {
                    var found = minio.BucketExistsAsync(bucketName);
                    if (found.Result)
                    {
                        observable = minio.ListIncompleteUploads(bucketName, prefix, recursive);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                    //IDisposable subscription = observable.Subscribe(
                    //    item => Console.WriteLine($"OnNext: {item.Key}"),
                    //    ex => Console.WriteLine($"OnError: {ex.Message}"),
                    //    () => Console.WriteLine($"Listed the pending uploads to bucket {bucketName}"));
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, observable);
            }

            #endregion

            #region 存储桶策略

            /// <summary>
            /// 获取存储桶或者对象前缀的访问权限
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            public async static Task<Tuple<bool, string>> GetPolicy(MinioClient minio, string bucketName)
            {
                bool flag = false;
                string policyJson = string.Empty;
                try
                {
                    var found = minio.BucketExistsAsync(bucketName);
                    if (found.Result)
                    {
                        policyJson = await minio.GetPolicyAsync(bucketName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, policyJson);
            }

            /// <summary>
            /// 针对存储桶和对象前缀设置访问策略
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            public async static Task<bool> SetPolicy(MinioClient minio, string bucketName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        string policyJson = $@"{{""Version"":""2012-10-17"",""Statement"":[{{""Action"":[""s3:GetBucketLocation""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:ListBucket""],""Condition"":{{""StringEquals"":{{""s3:prefix"":[""foo"",""prefix/""]}}}},""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:GetObject""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}/foo*"",""arn:aws:s3:::{bucketName}/prefix/*""],""Sid"":""""}}]}}";

                        await minio.SetPolicyAsync(bucketName, policyJson);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            #endregion

            #region 存储桶通知

            /// <summary>
            /// 获取存储桶的通知配置
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            private async static Task<Tuple<bool, string>> GetBucketNotification(MinioClient minio, string bucketName)
            {
                bool flag = false;
                string Ret = string.Empty;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        BucketNotification notifications = await minio.GetBucketNotificationsAsync(bucketName);
                        Ret = notifications.ToXML();
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, Ret);
            }

            /// <summary>
            /// 给存储桶设置通知
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            private async static Task<bool> SetBucketNotification(MinioClient minio, string bucketName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        BucketNotification notification = new BucketNotification();
                        Arn topicArn = new Arn("aws", "sns", "us-west-1", "412334153608", "topicminio");

                        TopicConfig topicConfiguration = new TopicConfig(topicArn);
                        List<EventType> events = new List<EventType>() { EventType.ObjectCreatedPut, EventType.ObjectCreatedCopy };
                        topicConfiguration.AddEvents(events);
                        topicConfiguration.AddFilterPrefix("images");
                        topicConfiguration.AddFilterSuffix("jpg");
                        notification.AddTopic(topicConfiguration);

                        QueueConfig queueConfiguration = new QueueConfig("arn:aws:sqs:us-west-1:482314153608:testminioqueue1");
                        queueConfiguration.AddEvents(new List<EventType>() { EventType.ObjectCreatedCompleteMultipartUpload });
                        notification.AddQueue(queueConfiguration);

                        await minio.SetBucketNotificationsAsync(bucketName, notification);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>
            /// 删除存储桶上所有配置的通知
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <returns></returns>
            private async static Task<bool> RemoveAllBucketNotifications(MinioClient minio, string bucketName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.RemoveAllBucketNotificationsAsync(bucketName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            #endregion

            #region 操作文件对象

            /// <summary>
            /// 从桶下载文件到本地
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="fileName">本地路径</param>
            /// <param name="sse"></param>
            /// <returns></returns>
            public async static Task<bool> FGetObject(MinioClient minio, string bucketName, string objectName, string fileName, ServerSideEncryption sse = null)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        if (System.IO.File.Exists(fileName))
                        {
                            System.IO.File.Delete(fileName);
                        }
                        await minio.GetObjectAsync(bucketName, objectName, fileName, sse).ConfigureAwait(false);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>
            /// 上传本地文件至存储桶
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="fileName">本地路径</param>
            /// <returns></returns>
            public async static Task<bool> FPutObject(MinioClient minio, string bucketName, string objectName, string fileName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.PutObjectAsync(bucketName, objectName, fileName, contentType: "application/octet-stream");
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            #endregion

            #region Presigned操作

            /// <summary>生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载，即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间，默认值是7天。
            /// 生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载，即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间，默认值是7天。
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="expiresInt">失效时间（以秒为单位），默认是7天，不得大于七天</param>
            /// <param name="reqParams">额外的响应头信息，支持response-expires、response-content-type、response-cache-control、response-content-disposition</param>
            /// <returns></returns>
            public async static Task<Tuple<bool, string>> PresignedGetObject(MinioClient minio, string bucketName, string objectName, int expiresInt = 1000)
            {
                bool flag = false;
                string Ret = string.Empty;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        var reqParams = new Dictionary<string, string> { { "response-content-type", "application/json" } };
                        string presignedUrl = await minio.PresignedGetObjectAsync(bucketName, objectName, expiresInt, reqParams);
                        Ret = presignedUrl;
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, Ret);
            }

            /// <summary>生成一个给HTTP PUT请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行上传，即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间，默认值是7天。
            /// 生成一个给HTTP PUT请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行上传，即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间，默认值是7天。
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="expiresInt">失效时间（以秒为单位），默认是7天，不得大于七天</param>
            /// <returns></returns>
            public async static Task<Tuple<bool, string>> PresignedPutObject(MinioClient minio, string bucketName, string objectName, int expiresInt = 1000)
            {
                bool flag = false;
                string Ret = string.Empty;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        string presignedUrl = await minio.PresignedPutObjectAsync(bucketName, objectName, expiresInt);
                        Ret = presignedUrl;
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, Ret);
            }

            /// <summary>允许给POST请求的presigned URL设置策略，比如接收对象上传的存储桶名称的策略，key名称前缀，过期策略。
            /// 允许给POST请求的presigned URL设置策略，比如接收对象上传的存储桶名称的策略，key名称前缀，过期策略。
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="PostPolicy">对象的post策略</param>
            /// <returns></returns>
            public async static Task<Tuple<bool, string, Dictionary<string, string>>> PresignedPostPolicy(MinioClient minio)
            {
                bool flag = false;
                Tuple<string, Dictionary<string, string>> tdic = null;
                try
                {
                    PostPolicy form = new PostPolicy();
                    DateTime expiration = DateTime.UtcNow;
                    form.SetExpires(expiration.AddDays(10));
                    form.SetKey("my-objectname");
                    form.SetBucket("my-bucketname");

                    Tuple<string, Dictionary<string, string>> tuple = await minio.PresignedPostPolicyAsync(form);
                    tdic = tuple;
                    flag = true;
                    //string curlCommand = "curl -X POST ";
                    //foreach (KeyValuePair<string, string> pair in tuple.Item2)
                    //{
                    //    curlCommand = curlCommand + $" -F {pair.Key}={pair.Value}";
                    //}
                    //curlCommand = curlCommand + " -F file=@/etc/bashrc " + tuple.Item1; // https://s3.amazonaws.com/my-bucketname";
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return Tuple.Create(flag, tdic.Item1, tdic.Item2);
            }

            #endregion

            #region 操作对象

            /// <summary>返回对象数据的流
            /// 返回对象数据的流
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="callback">处理流的回调函数</param>
            /// <returns></returns>
            public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, Action<Stream> callback)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.StatObjectAsync(bucketName, objectName);
                        await minio.GetObjectAsync(bucketName, objectName, callback);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>下载对象指定区域的字节数组做为流。offset和length都必须传
            /// 下载对象指定区域的字节数组做为流。offset和length都必须传
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="offset">offset 是起始字节的位置</param>
            /// <param name="length">length是要读取的长度</param>
            /// <param name="callback">处理流的回调函数</param>
            /// <returns></returns>
            public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, long offset, long length, Action<Stream> callback)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.StatObjectAsync(bucketName, objectName);
                        await minio.GetObjectAsync(bucketName, objectName, offset, length, callback);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>下载并将文件保存到本地文件系统
            /// 下载并将文件保存到本地文件系统
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="fileName">本地文件路径</param>
            /// <returns></returns>
            public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, string fileName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        if (System.IO.File.Exists(fileName))
                        {
                            System.IO.File.Delete(fileName);
                        }
                        await minio.StatObjectAsync(bucketName, objectName);
                        await minio.GetObjectAsync(bucketName, objectName, fileName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>通过文件上传到对象中
            /// 通过文件上传到对象中
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="filePath">要上传的本地文件名</param>
            /// <param name="contentType">文件的Content type，默认是"application/octet-stream"</param>
            /// <param name="metaData">元数据头信息的Dictionary对象，默认是null</param>
            /// <returns></returns>
            public async static Task<bool> PutObjectAsync(MinioClient minio, string bucketName, string objectName, string filePath, string contentType = "application/octet-stream", Dictionary<string, string> metaData = null)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.PutObjectAsync(bucketName, objectName, filePath, contentType, metaData);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>通过Stream上传对象
            /// 通过Stream上传对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <param name="data">要上传的Stream对象</param>
            /// <param name="size">流的大小</param>
            /// <param name="contentType">文件的Content type，默认是"application/octet-stream"</param>
            /// <param name="metaData">元数据头信息的Dictionary对象，默认是null</param>
            /// <returns></returns>
            public async static Task<bool> PutObjectAsync(MinioClient minio, string bucketName, string objectName, Stream data, long size, string contentType = "application/octet-stream", Dictionary<string, string> metaData = null)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        //byte[] bs = File.ReadAllBytes(fileName);
                        //System.IO.MemoryStream filestream = new System.IO.MemoryStream(bs);

                        await minio.PutObjectAsync(bucketName, objectName, data, size, contentType, metaData);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>获取对象的元数据
            /// 获取对象的元数据
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <returns></returns>
            public async static Task<bool> StatObject(MinioClient minio, string bucketName, string bucketObject)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        ObjectStat statObject = await minio.StatObjectAsync(bucketName, bucketObject);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>从objectName指定的对象中将数据拷贝到destObjectName指定的对象
            /// 从objectName指定的对象中将数据拷贝到destObjectName指定的对象
            /// </summary>
            /// <param name="minio"></param>
            /// <param name="fromBucketName">源存储桶名称</param>
            /// <param name="fromObjectName">源存储桶中的源对象名称</param>
            /// <param name="destBucketName">目标存储桶名称</param>
            /// <param name="destObjectName">要创建的目标对象名称,如果为空，默认为源对象名称</param>
            /// <param name="copyConditions">拷贝操作的一些条件Map</param>
            /// <param name="sseSrc"></param>
            /// <param name="sseDest"></param>
            /// <returns></returns>
            public async static Task<bool> CopyObject(MinioClient minio, string fromBucketName, string fromObjectName, string destBucketName, string destObjectName, CopyConditions copyConditions = null, ServerSideEncryption sseSrc = null, ServerSideEncryption sseDest = null)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(fromBucketName);
                    if (!found)
                    {
                        throw new Exception(string.Format("源存储桶[{0}]不存在", fromBucketName));
                    }
                    bool foundtwo = await minio.BucketExistsAsync(destBucketName);
                    if (!foundtwo)
                    {
                        throw new Exception(string.Format("目标存储桶[{0}]不存在", destBucketName));
                    }
                    await minio.CopyObjectAsync(fromBucketName, fromObjectName, destBucketName, destObjectName, copyConditions, null, sseSrc, sseDest);
                    flag = true;
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>删除一个对象
            /// 删除一个对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <returns></returns>
            public async static Task<bool> RemoveObject(MinioClient minio, string bucketName, string objectName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.RemoveObjectAsync(bucketName, objectName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>删除多个对象
            /// 删除多个对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectsList">含有多个对象名称的IEnumerable</param>
            /// <returns></returns>
            public static async Task<bool> RemoveObjects(MinioClient minio, string bucketName, List<string> objectsList)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        if (objectsList != null)
                        {
                            IObservable<DeleteError> objectsOservable = await minio.RemoveObjectAsync(bucketName, objectsList).ConfigureAwait(false);
                            flag = true;
                            //IDisposable objectsSubscription = objectsOservable.Subscribe(
                            //    objDeleteError => Console.WriteLine($"Object: {objDeleteError.Key}"),
                            //        ex => Console.WriteLine($"OnError: {ex}"),
                            //        () =>
                            //        {
                            //            Console.WriteLine($"Removed objects in list from {bucketName}\n");
                            //        });
                            //return;
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            /// <summary>删除一个未完整上传的对象
            /// 删除一个未完整上传的对象
            /// </summary>
            /// <param name="minio">连接实例</param>
            /// <param name="bucketName">存储桶名称</param>
            /// <param name="objectName">存储桶里的对象名称</param>
            /// <returns></returns>
            public async static Task<bool> RemoveIncompleteUpload(MinioClient minio, string bucketName, string objectName)
            {
                bool flag = false;
                try
                {
                    bool found = await minio.BucketExistsAsync(bucketName);
                    if (found)
                    {
                        await minio.RemoveIncompleteUploadAsync(bucketName, objectName);
                        flag = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
                    }
                }
                catch (MinioException e)
                {
                    throw new Exception(e.Message);
                }
                return flag;
            }

            #endregion
        }
    }
}